//
//  JSTPublishBar.m
//  KYKJPackDemo
//
//  Created by 01 on 17/5/16.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "JSTPublishBar.h"
#import "MessageView.h"

#define ShowkeyboardHeight 216
#define BarHeight kCurrentScreenWidth(44)
#define DurationTime 0.25

@interface JSTPublishBar ()<UIActionSheetDelegate,MessageViewDelegate>

@property (nonatomic, strong) UIView *sepView; //分割线

@property (nonatomic, strong) UITextView *pBtextView;

@property (nonatomic, strong) MessageView *messageView; //表情

@end

@implementation JSTPublishBar

- (instancetype)initWithFrame:(CGRect)frame andView:(UITextView *)myTextView {
    
    if (self = [super initWithFrame:frame]) {
        
        // 1.初始化属性
        self.pBtextView = myTextView;
        self.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
        
        _sepView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
        _sepView.backgroundColor = [UIColor colorWithHexString:@"9fa0a0"];
        [self addSubview:_sepView];

        [self setUpAllSubViews];
        
        // 2.注册键盘通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

#pragma mark - setUp
- (void)setUpAllSubViews
{
#warning 控制陌生人是否能评价  好友系统后开启
//    [self addSubview:self.strangerBut];
    
//    [self addSubview:self.strangerLabel];
    
    [self addSubview:self.showBut];
    
    [self addSubview:self.imageBut];
}

#pragma mark - 懒加载子控件
- (UIButton *)strangerBut
{
    if (!_strangerBut) {
        _strangerBut = [UIButton buttonWithType:UIButtonTypeCustom];
        _strangerBut.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
        [_strangerBut setImage:[UIImage imageNamed:@"healthy_choose_normal"] forState:UIControlStateNormal];
        [_strangerBut setImage:[UIImage imageNamed:@"my_order_comment_btn_choose_pressed"] forState:UIControlStateSelected];
        [_strangerBut addTarget:self action:@selector(strangerButPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _strangerBut;
}

- (UILabel *)strangerLabel
{
    if (!_strangerLabel) {
        _strangerLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_strangerBut.frame), kCurrentScreenWidth(10), kCurrentScreenWidth(100), kCurrentScreenWidth(24))];
        _strangerLabel.font = kSystemFont(15);
        _strangerLabel.textColor = [UIColor colorWithHexString:@"9fa0a0"];
        _strangerLabel.text = @"陌生人可评价";
    }
    return _strangerLabel;
}

- (UIButton *)showBut
{
    if (!_showBut) {
        _showBut = [UIButton buttonWithType:UIButtonTypeCustom];
        _showBut.frame = CGRectMake(self.frame.size.width - 2*self.frame.size.height-kCurrentScreenWidth(5), 0, self.frame.size.height, self.frame.size.height);
        [_showBut setImage:[UIImage imageNamed:@"healthy_comments_face"] forState:UIControlStateNormal];
        [_showBut setImage:[UIImage imageNamed:@"healthy_icon_keyboard"]  forState:UIControlStateSelected];
        [_showBut addTarget:self action:@selector(showButPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _showBut;
}

- (UIButton *)imageBut
{
    if (!_imageBut) {
        _imageBut = [UIButton buttonWithType:UIButtonTypeCustom];
        _imageBut.frame = CGRectMake(CGRectGetMaxX(_showBut.frame), 0, self.frame.size.height, self.frame.size.height);
        [_imageBut setImage:[UIImage imageNamed:@"healthy_icon_picture"] forState:UIControlStateNormal];
        [_imageBut addTarget:self action:@selector(imageButPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _imageBut;
}

- (MessageView *)messageView
{
    if (!_messageView) {
        _messageView = [[MessageView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, ShowkeyboardHeight)];
        _messageView.delegate = self;
    }
    return _messageView;
}

#pragma mark - 点击事件
- (void)strangerButPress:(UIButton *)but
{
    but.selected = !but.selected;
    if (_delegate&&[_delegate respondsToSelector:@selector(publishBarToStrangerWithBut:)]) {
        [_delegate publishBarToStrangerWithBut:but];
    }
}

- (void)showButPress:(UIButton *)but
{
    but.selected = !but.selected;
    if (but.selected) {
        [_pBtextView resignFirstResponder];
        [self.superview addSubview:self.messageView];
        [UIView animateWithDuration:DurationTime animations:^ {
            _messageView.frame = CGRectMake(0, kScreenHeight - ShowkeyboardHeight - 64, kScreenWidth, ShowkeyboardHeight);
            self.frame = CGRectMake(0, kScreenHeight - ShowkeyboardHeight - BarHeight- 64, kScreenWidth, BarHeight);
        }];
    } else {
        [_pBtextView becomeFirstResponder];
    }
}

- (void)imageButPress:(UIButton *)but
{
    [self.pBtextView resignFirstResponder];
    
    UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    [actionSheet showInView:[self superview]];
}

#pragma mark - 监听键盘的改变通知
- (void)keyboardWillShow:(NSNotification *)notification
{
    // 1.取出键盘的frame
    CGRect _keyboardRect=[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 2.取出键盘弹出的时间
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    _showBut.selected = NO;
    [UIView animateWithDuration:duration animations:^{
        
        self.frame = CGRectMake(0, kScreenHeight - CGRectGetHeight(_keyboardRect)-kCurrentScreenWidth(44) - 64, kScreenWidth, kCurrentScreenWidth(44));
        _messageView.frame = CGRectMake(0, kScreenHeight - 64, kScreenWidth, 216);
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0, kScreenHeight - kCurrentScreenWidth(44) - 64, kScreenWidth, kCurrentScreenWidth(44));
    }];
}

- (void)keyboardChange:(NSNotification *)notification
{
    
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (_delegate&&[_delegate respondsToSelector:@selector(getPublishBarToPickerWithTag:)]) {
        [_delegate getPublishBarToPickerWithTag:buttonIndex];
    }
}

#pragma mark - MessageViewDelegate
-(void)messageViewFaceWithButton:(UIButton *)but
{
    if (_delegate&&[_delegate respondsToSelector:@selector(publishBarWithBut:)]) {
        [_delegate publishBarWithBut:but];
    }
}
@end
